package com.vanke.devops.api.controller.docker;

import com.github.dockerjava.api.model.Container;
import com.vanke.core.bean.ApiBaseResponse;
import com.vanke.core.util.JsonUtil;
import com.vanke.devops.api.base.BaseController;
import com.vanke.devops.domain.domain.docker.DockerService;
import com.vanke.devops.domain.domain.docker.dto.ServiceDTO;
import com.vanke.devops.domain.domain.docker.dto.TaskDTO;
import com.vanke.devops.domain.manager.docker.DockerManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈景阳
 * @since 2018/4/1
 */
@RestController
@RequestMapping("/docker")
public class DockerController extends BaseController{

    @Autowired
    private DockerManager dockerManager;

    @Autowired
    private DockerService dockerService;

    @GetMapping("/ps/{instanceId}")
    public ApiBaseResponse ps(@PathVariable("instanceId") String instanceId){
        List<Container> list = dockerManager.ps(instanceId);
        return setResponse(list);
    }

    @GetMapping("/stats")
    public ApiBaseResponse stats(@RequestParam("ip") String ip){
        dockerManager.stats(ip);
        return setResponse();
    }

    @GetMapping("/services")
    public ApiBaseResponse services(@RequestParam("ip") String ip,
                                    @RequestParam("keyword") String keyword){
        List<ServiceDTO> list = dockerService.services(ip,keyword);
        return setResponse(list);
    }

    @GetMapping("/services/{id}")
    public ApiBaseResponse getServiceByName(@RequestParam("ip") String ip,
                                            @PathVariable("id")String id){
        ServiceDTO dto = dockerService.getServiceById(ip,id);
        return setResponse(dto);
    }

    @GetMapping("/tasks/{serviceName}")
    public ApiBaseResponse getTasksByServiceName(@RequestParam("ip") String ip,
                                                 @PathVariable("serviceName") String serviceName){
        List<TaskDTO> list = dockerService.getTasksByServiceName(ip, serviceName);
        return setResponse(list);
    }

}
